package org.tgcloud.group.web.controller.app;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.app.OrderService;
import org.tgcloud.group.web.controller.dto.order.ManagerOrderDTO;
import org.tgcloud.group.web.controller.req.order.*;
import org.tgcloud.group.web.controller.req.page.SearchAppPageReq;
import org.tgcloud.group.web.controller.req.project.UpdateProjectOutGoodReq;

import java.util.List;

/**
 * @author: zhy
 * @create: 2023-08-16 13:13
 * @description: 订单相关接口
 */
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/app/user")
@EnableAutoConfiguration
public class OrderController extends ClientAPPBaseController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "订单列表")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/orderList")
    public APPResultMessage<Object, ManagerOrderDTO> getOrderList(@ModelAttribute("session")Session session,
                                                                @RequestBody SearchAppPageReq req) {
        return orderService.doGetOrderList(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-渠道核价通知---推送给渠道")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/checkChannelMsg")
    public APPResultMessage<Object, List> doCheckChannelMsg(@ModelAttribute("session")Session session,
                                                                  @RequestBody CheckChannelUserDTO req) {
        if (StringUtil.isBlank(req.getUserIds())){
            return APPResultMessage.MISS_PARAMETER();
        }
        return orderService.doCheckChannelMsg(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-订单详情")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/getOrderMsg")
    public APPResultMessage<ManagerOrderDTO, List> getOrderMsg(@ModelAttribute("session")Session session,
                                                            @RequestBody OrderIdReq req) {
        return orderService.getOrderMsg(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-转移渠道开拓者")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/transferPushUsers")
    public APPResultMessage<Object, List> doTransferPushUsers(@ModelAttribute("session")Session session,
                                                            @RequestBody CheckChannelUserDTO req) {
        if (StringUtil.isBlank(req.getUserIds())){
            return APPResultMessage.MISS_PARAMETER();
        }
        return orderService.doTransferPushUsers(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-盲盒核价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/blindBoxRate")
    public APPResultMessage<Object, List> doBlindBoxRate(@ModelAttribute("session")Session session,
                                                              @RequestBody BlindBoxRateReq req) {
        return orderService.doBlindBoxRate(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-渠道提交核价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/channelCheckPrice")
    public APPResultMessage<Object, List> doChannelCheckPrice(@ModelAttribute("session")Session session,
                                                                @RequestBody ChannelCheckPriceReq req) {
        if (StringUtil.isBlank(req.getGoods())){
            return APPResultMessage.MISS_PARAMETER();
        }
        return orderService.doChannelCheckPrice(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-开拓者确认渠道核价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/sureChannelCheckPrice")
    public APPResultMessage<Object, List> doSureChannelCheckPrice(@ModelAttribute("session")Session session,
                                                              @RequestBody OrderSureCheckReq req) {
//        if (StringUtil.isBlank(req.getCheckRemark()) || StringUtil.isBlank(req.getCheckCashPrice()) || StringUtil.isBlank(req.getCheckCostPrice()) ||
//                req.getCashDiscountType() == null || req.getCostDiscountType() == null
//        ){
//            return APPResultMessage.MISS_PARAMETER();
//        }
        return orderService.doSureChannelCheckPrice(session.getUserId(), session.getUserType(),req);
    }


    @ApiOperation(value = "订单-开拓者确认一次竞价的结束时间")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/sureBiddingEndTime")
    public APPResultMessage<Object, List> doSureBiddingEndTime(@ModelAttribute("session")Session session,
                                                                  @RequestBody BiddingEndTimeReq req) {
        return orderService.doSureBiddingEndTime(session.getUserId(),req);
    }



    @ApiOperation(value = "订单-开拓者推送参与竞价的渠道用户")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/surePushChannelUser")
    public APPResultMessage<Object, List> doSurePushChannelUser(@ModelAttribute("session")Session session,
                                                                  @RequestBody CheckChannelUserDTO req) {
        return orderService.doSurePushChannelUser(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-开拓者修改一次竞价结束时间")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/updateSureBiddingEndTime")
    public APPResultMessage<Object, List> doUpdateSureBiddingEndTime(@ModelAttribute("session")Session session,
                                                               @RequestBody BiddingEndTimeReq req) {
        return orderService.doUpdateSureBiddingEndTime(session.getUserId(),req);
    }

    @ApiOperation(value = "订单-渠道用户发起二次竞价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/startTwoBidding")
    public APPResultMessage<Object, List> doStartTwoBidding(@ModelAttribute("session")Session session,
                                                                @RequestBody OrderIdReq req) {
        return orderService.doStartTwoBidding(session.getUserId(),req);
    }

    @ApiOperation(value = "订单-渠道用户参与二次竞价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/joinTwoBidding")
    public APPResultMessage<Object, List> doJoinTwoBidding(@ModelAttribute("session")Session session,
                                                            @RequestBody OrderIdReq req) {
        return orderService.doJoinTwoBidding(session.getUserId(),req);
    }

    @ApiOperation(value = "订单-开拓者拒绝订单")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/refuseOrder")
    public APPResultMessage<Object, List> doRefuseOrder(@ModelAttribute("session")Session session,
                                                           @RequestBody RefuseOrderReq req) {
        return orderService.doRefuseOrder(session.getUserId(),req);
    }

    @ApiOperation(value = "订单-修改盲盒实收，卖货实收")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/updateOrderRealPrice")
    public APPResultMessage doUpdateOrderRealPrice(@ModelAttribute("session")Session session,
                                                   @RequestBody UpdateOrderRealPriceReq req) {
        return orderService.doUpdateOrderRealPrice(session.getUserId(),req);
    }

    @ApiOperation(value = "订单-修改出货信息")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/updateProjectOutGoodMsg")
    public APPResultMessage doUpdateProjectOutGoodMsg(@ModelAttribute("session")Session session,
                                                   @RequestBody UpdateProjectOutGoodReq req) {
        return orderService.doUpdateProjectOutGoodMsg(session.getUserId(),req);
    }


    @ApiOperation(value = "订单-渠道拒绝核价/竞价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/doRefuseChannelOrder")
    public APPResultMessage doRefuseChannelOrder(@ModelAttribute("session")Session session,
                                                      @RequestBody OrderIdReq req) {
        return orderService.doRefuseChannelOrder(session.getUserId(),req);
    }



    @ApiOperation(value = "小程序首页待处理，竞价中的数量")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/getHomeShowNum")
    public APPResultMessage getHomeShowNum(@ModelAttribute("session")Session session) {
        return orderService.getHomeShowNum2(session.getUserId());
    }


    @ApiOperation(value = "订单-开拓者重置订单-重置推送核价")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/doReSettingOrder")
    public APPResultMessage doReSettingOrder(@ModelAttribute("session")Session session,
                                                 @RequestBody OrderIdReq req) {
        return orderService.doReSettingOrder(session.getUserId(),req.getOrderId());
    }


    @ApiOperation(value = "订单-开拓者重置订单-重置至设置报价时间")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/doReSettingOrderTime")
    public APPResultMessage doReSettingOrderTime(@ModelAttribute("session")Session session,
                                             @RequestBody OrderIdReq req) {
        return orderService.doReSettingOrderTime(session.getUserId(),req.getOrderId());
    }

    @ApiOperation(value = "订单-竞价后渠道放弃订单")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/abandonOrder")
    public APPResultMessage abandonOrder(@ModelAttribute("session")Session session,
                                                      @RequestBody OrderIdReq req) {
        return orderService.abandonOrder(session.getUserId(),req, session.getUserType());
    }

    @ApiOperation(value = "回复商务超时订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/replyTimeOutRemark", method = RequestMethod.POST)
    public APPResultMessage replyTimeOutRemark(@ModelAttribute("session") Session session,
                                               @RequestBody OrderReplyReq orderReplyReq
    ){
        return orderService.replyTimeOutRemark(session.getUserId(),orderReplyReq.getOrderId(),orderReplyReq.getContent());
    }

    @ApiOperation(value = "订单评级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doSetAppOrderRate", method = RequestMethod.POST)
    public APPResultMessage doSetAppOrderRate(@ModelAttribute("session") Session session,
                                               @RequestBody OrderRateReq rateReq){
        return orderService.doSetAppOrderRate(session.getUserId(),rateReq.getOrderId(),rateReq.getRateId());
    }

}
